<?php
require_once('../includes/settings.php');
require_once('../includes/functions.php');
require_once('../includes/repository.php');

deny_anonymous();

$user_id = user_val('id');

function words_to_learn($user_id){
	$sql = "SELECT voca_user_words.word_id,familiarity,word,definition FROM `voca_user_words`".
		" LEFT JOIN `voca_words` ON voca_user_words.word_id = voca_words.id".
		" LEFT JOIN `voca_definitions` ON voca_definitions.word_id = voca_words.id".
		" AND (voca_definitions.user_id = voca_user_words.user_id or is_standard=1)".
		" WHERE voca_user_words.user_id=? AND familiarity<5".
		" ORDER BY created_at ASC".
		" LIMIT 0, ".user_val('amount_per_day');
	$stmt = Repository::prepare($sql);
	$stmt -> bind_param('i', $user_id);
	$stmt -> execute();
	$rs = Repository::get_result($stmt);
	return $rs;

}

function divide(&$result,$word){
	$result[$word['familiarity']][]=array("w"=>$word['word'],"d"=>$word['definition']);
}

function course_info($user_id,$words,$completed){
	$info=array(array(),array(),array(),array(),array());
	$info[0]['total']=0;
	$info[0]['done']=$completed;
	foreach ($words as $word) {
		$info[0]['total']=$info[0]['total']+1;
		divide($info,$word);
	}
	$info[0]['undone']=$info[0]['total']-$info[0]['done'];
	return $info;
}

function get_completed_count($user_id){
	$sql = 'SELECT count(*) count FROM voca_daily_records r'.
		' LEFT JOIN voca_daily_learning_items i ON r.id = i.record_id'.
		' WHERE user_id=? AND DATE(created_at) = CURDATE() AND passed=1';
	$stmt = Repository::prepare($sql);
	$stmt -> bind_param('i',$user_id);
	$stmt -> execute();
	$result = Repository::get_result($stmt,TRUE);
	if(count($result)==1){
		return $result['count'];
	}
	return 0;
}

function create_daily_record($user_id){
	$sql = 'INSERT INTO `voca_daily_records` (user_id,created_at) VALUES(?,now())';
	$stmt = Repository::prepare($sql);
	$stmt -> bind_param('i',$user_id);
	$stmt -> execute();
	Repository::rollback_if_failed();
	return Repository::last_insert_id();
}

function get_record_id($user_id){
	$sql = 'SELECT id FROM voca_daily_records WHERE user_id=? AND DATE(created_at) = CURDATE()';
	$stmt = Repository::prepare($sql);
	$stmt -> bind_param('i',$user_id);
	$stmt -> execute();
	$rs = Repository::get_result($stmt,TRUE);
	if(is_array($rs)&&count($rs)>0){
		return $rs['id'];
	}
	return 0;
}

function init_course($user_id,$words){
	$record_id = create_daily_record($user_id);
	$sql= "INSERT INTO `voca_daily_learning_items` (record_id,word_id,familiarity) VALUES(?,?,?)";
	$stmt = Repository::prepare($sql);
	foreach ($words as $word) {
		$stmt -> bind_param('iii',$record_id,$word['word_id'],$word['familiarity']);
		$stmt -> execute();
		Repository::rollback_if_failed();
	}
	Repository::commit();
	return $record_id;
}

if(isset($_REQUEST['action'])){
	$action = strtolower($_REQUEST['action']);
	if($action=='info'){
		$words = words_to_learn($user_id);
		if(count($words)==0){
			_jsonp(TRUE, array(0));
		}else{
			$completed = get_completed_count($user_id);
			$info = course_info($user_id,$words,$completed);
			_jsonp(TRUE,$info);
		}
		Repository::close();
	}elseif($action=='initialize'){
		$record_id = get_record_id($user_id);
		if($record_id>0){
			_jsonp(TRUE,array("record_id" => $record_id));
		}else{
			$words = words_to_learn($user_id);
			if(count($words)==0){
				_jsonp(FALSE);
			}else{
				$record_id = init_course($user_id,$words);
				_jsonp(TRUE,array("record_id" => $record_id));
			}
			Repository::close();
		}
	}
}
exit();
?>